﻿using System.Data;
using System.IO;
using DataScripter.Database.SqlServer;
using DataScripter.Scripting.OutputDestinations;

namespace DataScripter.Scripting.Output.Sql.SqlServer.SqlServer2008
{
    public class InsertOutputProducer : SqlServer.InsertOutputProducer
    {
        public InsertOutputProducer(IOutputDestination container) : base(container)
        {
        }

        protected override void ScriptBeforeFirstRecord(ITableScriptRequest tableScriptRequest, StreamWriter streamWriter)
        {
            streamWriter.WriteLine(ProduceInsertClause(tableScriptRequest, true));
        }

        protected override void HandleRecord(IDataReader reader, StreamWriter streamWriter, bool isFirstRecord)
        {
            ScriptTableNamePkFound(TableScriptRequest, reader, streamWriter);
            ScriptSpecialFieldTreatments(TableScriptRequest, reader, streamWriter, OutputContext, OutputDestination.Resolve<SqlServerDb>());
            if (!isFirstRecord)
            {
                streamWriter.Write(",");
            }
            streamWriter.WriteLine(ProduceValuesSqlForCurrentRecord(TableScriptRequest, reader, isFirstRecord));
        }
    }
}